package com.cqupt.qualityevaluation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqupt.qualityevaluation.controller.VO.schoolVO;
import com.cqupt.qualityevaluation.dao.*;
import com.cqupt.qualityevaluation.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/school")
public class SchoolController {
    @Autowired
    private schoolMapper schoolMapper;
    @Autowired
    private schooltestMapper schooltestMapper;
    @Autowired
    private schoolhostcodeMapper schoolhostcodeMapper;
    @Autowired
    private SchooltypecodeMapper schooltypecodeMapper;
    @Autowired
    private schoollocationtypecodeMapper schoollocationtypecodeMapper;
    //展示所有学校
    @GetMapping("/findAll")
    public List<school> findAll(){
        return schoolMapper.selectList(null);
    }

    //分页功能
    @GetMapping("/page")
    public schoolVO queryList(Integer current, Integer size){
        schoolVO schoolVO=new schoolVO();
        IPage<school> page=new Page<>(current,size);
        schoolMapper.selectPage(page,null);
        schoolVO.setCurrent(current);
        schoolVO.setSize(size);
        schoolVO.setTotal(page.getTotal());
        schoolVO.setSchoolList(page.getRecords());
        return schoolVO;
    }
    //接收前端传来的数据 进行插入
    @PostMapping("/save")
    //通过RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的),将JSON对象转换为java对象
    public Object save(@RequestBody school s){
        Map map=new HashMap<>();
        int result=0;
        try {
            result=schoolMapper.insert(s);
        }catch (org.springframework.dao.DuplicateKeyException e){
            map.put("code","fail");
            map.put("msg","该学校名称已存在");
            return map;
        }
        if (result==1){
            map.put("code","success");
            map.put("msg","添加成功");
        }
        else {
            map.put("code", "fail");
            map.put("msg","添加失败");
        }
        return map;
    }
    @DeleteMapping("/delete/{schoolname}")
    public String delete(@PathVariable("schoolname") String schoolname){
        QueryWrapper<school> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schoolname",schoolname);
        schoolMapper.delete(queryWrapper);
        return "success";
    }
    //更新信息 使用PutMapping
    @PutMapping("/update")
    public Object update(@RequestBody school s){
        Map map=new HashMap<>();
        int result=0;
        UpdateWrapper<school> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",s.getId());
        school school=new school();
        school.setSchoolhostcode(s.getSchoolhostcode());
        school.setSchoolname(s.getSchoolname());
        school.setSchooltypecode(s.getSchooltypecode());
        school.setSchoolcode(s.getSchoolcode());
        school.setSchoollocationcode(s.getSchoollocationcode());
        school.setSchoollocationname(s.getSchoollocationname());
        school.setSchoollocationtypecode(s.getSchoollocationtypecode());
        school.setSchoolhostname(s.getSchoolhostname());
        school.setSchoollocationtypename(s.getSchoollocationtypename());
        school.setSchooltypename(s.getSchooltypename());
        school.setPassword(s.getPassword());
        try{
            result=schoolMapper.update(school,updateWrapper);
        }catch (org.springframework.dao.DuplicateKeyException e){
            map.put("code","fail");
            map.put("msg","该学校名称已存在");
            return map;
        }
        if (result==1){
            map.put("code","success");
            map.put("msg","更新成功");
        }
        else {
            map.put("code", "fail");
            map.put("msg","添加失败");
        }
        return map;
    }

    //查询单行数据（前端点击修改即回显数据到表单中）操作
    @GetMapping("/selectone/{id}")
    public school findBySchoolId(@PathVariable("id") String id){
        QueryWrapper<school> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return schoolMapper.selectOne(queryWrapper);
    }
    @GetMapping("/selectone/shc/{schoolhostcode}")
    public schoolhostcode selectBySchoolHostCode(@PathVariable("schoolhostcode") String schoolhostcode){
        QueryWrapper<schoolhostcode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schoolhostcode",schoolhostcode);
        return schoolhostcodeMapper.selectOne(queryWrapper);
    }
    @GetMapping("/selectone/stc/{schooltypecode}")
    public schooltypecode selectBySchoolTypeCode(@PathVariable("schooltypecode") String schooltypecode){
        QueryWrapper<schooltypecode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schooltypecode",schooltypecode);
        return schooltypecodeMapper.selectOne(queryWrapper);
    }
    @GetMapping("/selectone/sltc/{schoollocationtypecode}")
    public schoollocationtypecode selectBySchoolLocationTypeCode(@PathVariable("schoollocationtypecode") String schoollocationtypecode){
        QueryWrapper<schoollocationtypecode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schoollocationtypecode",schoollocationtypecode);
        return schoollocationtypecodeMapper.selectOne(queryWrapper);
    }
    @GetMapping("/search/1/{schoolcode}")
    public List<school> findBySchoolCode(@PathVariable("schoolcode") String schoolcode){
        QueryWrapper<school> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("schoolcode",schoolcode);
        return schoolMapper.selectList(queryWrapper);
    }
    @GetMapping("/search/2/{schoolname}")
    public List<school> findBySchoolName(@PathVariable("schoolname") String schoolname){
        QueryWrapper<school> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("schoolname",schoolname);
        return schoolMapper.selectList(queryWrapper);
    }
    @GetMapping("/search/3/{schoollocationname}")
    public List<school> findBySchoolLocationName(@PathVariable("schoollocationname") String schoollocationname){
        QueryWrapper<school> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("schoollocationname",schoollocationname);
        return schoolMapper.selectList(queryWrapper);
    }
    @GetMapping("/search/4/{schoollocationcode}")
    public List<school> findBySchoolLocationCode(@PathVariable("schoollocationcode") String schoollocationcode){
        QueryWrapper<school> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("schoollocationcode",schoollocationcode);
        return schoolMapper.selectList(queryWrapper);
    }
    @GetMapping("/search/5/{schoollocationtypecode}")
    public List<school> findBySchoolLocationTypeCode(@PathVariable("schoollocationtypecode") String schoollocationtypecode){
        QueryWrapper<school> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("schoollocationtypecode",schoollocationtypecode);
        return schoolMapper.selectList(queryWrapper);
    }
    @GetMapping("/search/6/{schooltypecode}")
    public List<school> findBySchoolTypeCode(@PathVariable("schooltypecode") String schooltypecode){
        QueryWrapper<school> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("schooltypecode",schooltypecode);
        return schoolMapper.selectList(queryWrapper);
    }
    @GetMapping("/search/7/{schoolhostcode}")
    public List<school> findBySchoolHostCode(@PathVariable("schoolhostcode") String schoolhostcode){
        QueryWrapper<school> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("schoolhostcode",schoolhostcode);
        return schoolMapper.selectList(queryWrapper);
    }
    @GetMapping("/searchmorethanone/{schoolcode}/{schoolname}/{schoollocationname}/{schoollocationtypename}/{schooltypename}/{schoolhostname}")
    public List<school> findByMore(@PathVariable("schoolcode") String schoolcode,
                                   @PathVariable("schoolname") String schoolname,
                                   @PathVariable("schoollocationname") String schoollocationname,
                                   @PathVariable("schoollocationtypename") String schoollocationtypename,
                                   @PathVariable("schooltypename") String schooltypename,
                                   @PathVariable("schoolhostname") String schoolhostname){
        String newschoolhostname = null;
        String newschoolcode=null;
        String newschoolname=null;
        String newschoollocationname=null;
        String newschoollocationtypename=null;
        String newschooltypename=null;

        if (schoolcode.equals("all")){
            newschoolcode=schoolcode.replaceAll("all","");
        }
        else {
            newschoolcode=schoolcode;
        }
        if (schoolname.equals("all")){
            newschoolname=schoolname.replaceAll("all","");
        }
        else {
            newschoolname=schoolname;
        }
        if (schoollocationname.equals("all")){
            newschoollocationname=schoollocationname.replaceAll("all","");
        }
        else {
            newschoollocationname=schoollocationname;
        }
        if (schooltypename.equals("all")){
            newschooltypename=schooltypename.replaceAll("all","");
        }
        else {
            newschooltypename=schooltypename;
        }
        if (schoollocationtypename.equals("all")){
            newschoollocationtypename=schoollocationtypename.replaceAll("all","");
        }
        else{
            newschoollocationtypename=schoollocationtypename;
        }
        if (schoolhostname.equals("all")){
            newschoolhostname=schoolhostname.replaceAll("all","");
        }
        else {
            newschoolhostname=schoolhostname;
        }
        QueryWrapper<school> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("schoolcode",newschoolcode)
                .like("schoolname",newschoolname)
                .like("schoollocationname",newschoollocationname)
                .like("schoollocationname",newschoollocationname)
                .like("schoollocationtypename",newschoollocationtypename)
                .like("schooltypename",newschooltypename)
                .like("schoolhostname",newschoolhostname);
        return schoolMapper.selectList(queryWrapper);
    }
}
